/*
 * @Autor: xgq
 * @Description: 
 * @Version: 1.0
 * @Date: 2020-05-11 14:18:40
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-05-26 18:42:23
 */

/*
  用户操作控制器
  登录，注销，修改密码
*/
var express = require('express');
var account_router = express.Router();
var db = require('../../models')
var auth = require('../../comm/auth')

/**
 * @author: xgq
 * @description: 管理者登录
 * @param {type} 
 * @return: 
 */
account_router.post('/login', async (req, res)=>{
  var result = { 'code': 500 }
  var name = req.body.username,
  txtPwd = req.body.pwd;
  // txtVailCode = req.body.vailCode;
  let entext = auth.encryption(txtPwd)

  var querydata = await db.admin.findOne({where: {"name": name}});
  if( querydata != null && querydata.pwd ==  entext){
      req.session.regenerate(function(err) {
          if(err){
              return res.json(result)
          }
          let id=querydata.id
          result.code = 200;
          result.name = name
          result.token = auth.encryption(name)
          req.session.adminLoginUser = {"id":id,"name":name, "role": querydata.role};
          res.json(result)
      });
  }else{
    res.json(result)
  }
});



/**
 * @author: xgq
 * @description: 
 * @param {type} 
 * @return: 
 */
account_router.get('/get_admin_info',async (req, res)=>{
  var result = { 'code': 200, 'data':{
    'roles':[],
    'name':'tuyibo',
    'avatar':'',
    'introduction':'管理员'
  } }
  if(req.session.adminLoginUser!==undefined){
    if(req.session.adminLoginUser.role===0){
      result.data.roles.push('admin')
      result.data.roles.push('operator')
      result.data.introduction="管理员"
    }else{
      result.data.roles.push('operator')
      result.data.introduction="操作员"
    }
    result.data.name=req.session.adminLoginUser.name
  }

  res.json(result)
})

  

/**
 * @author: xgq
 * @description: 
 * @param {type} 
 * @return: 
 */
account_router.post('/logout', async (req, res)=>{
    auth.logout_admin_user(req)
    res.json({"code": 200})
})

/**
 * @author: xgq
 * @description: 
 * @param {type} 
 * @return: 
 */
account_router.post('/changePassword', async (req, res)=>{
  var result = { 'code': 500 }
  var id=req.session.adminLoginUser.id
  
  let{username,oldPassword,newPassword}=req.body
  let entext = auth.encryption(oldPassword)
  newPassword = auth.encryption(newPassword)
  var querydata = await db.admin.findOne({where:{"id":id,"name":username,"pwd":entext}});
  if( querydata != null && querydata.pwd ==  entext){
    db.admin.update({pwd:newPassword},{where:{"id":id,"name":username}}).then(res=>{
      result.code=200;
      res.json(result)
    }).catch(()=>{
      res.json(result)
    })
  }else{
    res.json(result)
  }
})


/**
 * @author: xgq
 * @description: 获取管理员列表
 * @param {type} 
 * @return: 
 */
account_router.post('/getOperatorList',async(req,res)=>{
  var list = await db.admin.getOperatorList(req.body)
  res.json(list)
})
/**
 * @author: xgq
 * @description: 
 * @param {type} 
 * @return: 
 */
account_router.post('/updateOperator',async(req,res)=>{
  var list = await db.admin.updateOperator(req.body)
  res.json(list)
})
/**
 * @author: xgq
 * @description: 
 * @param {type} 
 * @return: 
 */
account_router.post('/deleteOperator',async(req,res)=>{
  var list = await db.admin.deleteOperator(req.body)
  res.json(list)
})

/**
 * @author: xgq
 * @description: 
 * @param {type} 
 * @return: 
 */
account_router.post('/addOperator',async(req,res)=>{
  var obj = req.body
  obj.pwd = auth.encryption(req.body.password)
  var list=await db.admin.addOperator(obj)
  res.json(list)
})

module.exports = account_router